package com.aimosheng.main.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aimosheng.common.Constants;
import com.aimosheng.main.R;
import com.aimosheng.main.bean.TaskItemBean;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxf on 2018/10/23.
 */

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.Vh> {

    private List<TaskItemBean> mList;
    private LayoutInflater mInflater;
    private View.OnClickListener mOnClickListener;
    private View.OnClickListener mBtnClickListener;
    private ActionListener mActionListener;

    public TaskAdapter(Context context) {
        mList = new ArrayList<>();
        mInflater = LayoutInflater.from(context);
        mOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object tag = v.getTag();
                if (tag != null && mActionListener != null) {
                    int position = (int) tag;
                    mActionListener.onItemClick(mList.get(position), position);
                }
            }
        };
        mBtnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object tag = v.getTag();
                if (tag != null && mActionListener != null) {
                    int position = (int) tag;
                    mActionListener.onItemChildClick(mList.get(position), position);
                }
            }
        };
    }


    @NonNull
    @Override
    public Vh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_task, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Vh holder, int position) {

    }

    @Override
    public void onBindViewHolder(@NonNull Vh vh, int position, @NonNull List<Object> payloads) {
        Object payload = payloads.size() > 0 ? payloads.get(0) : null;
        vh.setData(mList.get(position), position, payload);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public void removeItem(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mList.size(), Constants.PAYLOAD);
    }

    public void insertItem(TaskItemBean bean) {
        int position = mList.size();
        mList.add(bean);
        notifyItemInserted(position);
    }

    public void setActionListener(ActionListener actionListener) {
        mActionListener = actionListener;
    }

    public void setList(List<TaskItemBean> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void getItem(int postion) {
        mList.get(postion);
    }



    class Vh extends RecyclerView.ViewHolder {

        TextView mName;
        TextView mTips;
        TextView mBtnTask;

        public Vh(View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.name);
            mTips = itemView.findViewById(R.id.tips);
            mBtnTask = itemView.findViewById(R.id.btn_task);
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setColor(Color.parseColor("#BF45F0"));
            gradientDrawable.setCornerRadius(DensityUtil.dp2px(30));
            mBtnTask.setBackground(gradientDrawable);
            mBtnTask.setOnClickListener(mBtnClickListener);
            itemView.setOnClickListener(mOnClickListener);
        }

        void setData(TaskItemBean bean, int position, Object payload) {
            itemView.setTag(position);
            mBtnTask.setTag(position);
            if (payload == null) {
                mName.setText(bean.getName());
                mTips.setText(bean.getTip());
                mBtnTask.setText(bean.getBtnText());
            }
        }
    }

    public interface ActionListener {
        void onItemClick(TaskItemBean bean, int position);
        void onItemChildClick(TaskItemBean bean, int position);
    }

}
